
console.log('Content script loaded');

window.onmessage = function (e) {
  if(e.data.type == 'pageToken'){
    chrome.runtime.sendMessage({
      type: 'pageToken',
      data: e.data.data
    })
  }
}

// 获取popup.js得getToken
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  console.log('content-script:',request);
  if (request.type === 'getToken') {
    window.postMessage({type: 'getToken',}, '*');
  }
  sendResponse("ok")
});

function injectCustomJs (jsPath) {
  jsPath = jsPath || 'script/inject.js';
  var temp = document.createElement('script');
  temp.setAttribute('type', 'text/javascript');
  temp.src = chrome.runtime.getURL(jsPath);
  temp.onload = function () {
    this.parentNode.removeChild(this);
  };
  document.body.appendChild(temp);
}

injectCustomJs();